/*

dp[i][j]=将[i,j]合并为一堆时的最大总能量
dp[i][j]=max(
    dp[i][j],
    dp[i][k]+dp[k+1][j]+i.m*k.n*j.n
)

dp[i][j]=0

*/
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <vector>
using ll = int64_t;

#define pvp(v)do{\
        std::cout<<#v<<":\n";\
        for(const auto&i:(v)){\
            std::cout<<i.first<<", "<<i.second<<"\n";\
        }\
    }while(0)

int main(){
    ll n;
    std::cin>>n;
    std::vector<ll> h(2*n+1);
    std::vector<std::vector<ll>> dp(2*n+1,std::vector<ll>(2*n+1));

    for(ll i=1;i<=n;++i){
        std::cin>>h[i];
        h[n+i]=h[i];
    }
    // pvp(v);
    for(ll len=2;len<=n;++len){
        for(ll i=1;i<=2*n-len;++i){
            ll j=i+len-1;
            for(ll k=i;k<j;k++){
                dp[i][j]=std::max(
                    dp[i][j],
                    dp[i][k]+dp[k+1][j]+h[i]*h[k+1]*h[j+1]
                );
            }
        }
    }
    ll ans=0;
    for(ll i=1;i<=n;i++){
        ans=std::max(
            ans,
            dp[i][i+n-1]
        );
    }
    std::cout<<ans<<"\n";
}